package com.atguigu.gulimall.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.atguigu.gulimall.product.entity.PmsAttrEntity;
import com.atguigu.gulimall.product.service.PmsAttrAttrgroupRelationService;
import com.atguigu.gulimall.product.service.PmsAttrService;
import com.atguigu.gulimall.product.service.PmsCategoryService;
import com.atguigu.gulimall.product.vo.AttrGroupRelationVo;
import com.atguigu.gulimall.product.vo.AttrGroupWithAttrsVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.atguigu.gulimall.product.entity.PmsAttrGroupEntity;
import com.atguigu.gulimall.product.service.PmsAttrGroupService;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;



/**
 * 属性分组
 *
 * @author yaotingkun
 * @email 1371364676@qq.com
 * @date 2021-10-13 14:21:50
 */
@RestController
@RequestMapping("product/attrgroup")
public class PmsAttrGroupController {
    @Autowired
    private PmsAttrGroupService pmsAttrGroupService;
    @Autowired
    private PmsCategoryService pmsCategoryService;
    @Autowired
    private PmsAttrService pmsAttrService;
    @Autowired
    private PmsAttrAttrgroupRelationService pmsAttrAttrgroupRelationService;

    @ApiOperation(value = "列表(默认)")
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = pmsAttrGroupService.queryPage(params);
        return R.ok().put("page", page);
    }

    @ApiOperation(value = "")
    @GetMapping("/{catelogId}/withattr")
    public R getAttrGroupWithAttrs(@PathVariable("catelogId")Long catelogId){
        //1、查出当前分类下的所有属性分组，
        //2、查出每个属性分组的所有属性
        List<AttrGroupWithAttrsVo> vos =  pmsAttrGroupService.getAttrGroupWithAttrsByCatelogId(catelogId);
        return R.ok().put("data",vos);
    }

    @ApiOperation(value = "平台属性--关联--新建关联--确认新增按钮")
    @PostMapping("/attr/relation")
    public R addRelation(@RequestBody List<AttrGroupRelationVo> vos){
        pmsAttrAttrgroupRelationService.saveBatchAttrRelation(vos);
        return R.ok();
    }

    @ApiOperation(value = "关联--新建关联信息--获取当前分组没有关联的所有属性")
    @GetMapping("/{attrgroupId}/noattr/relation")
    public R attrNoRelation(@PathVariable("attrgroupId") Long attrgroupId,
                            @RequestParam Map<String, Object> params){
        PageUtils page = pmsAttrService.getNoRelationAttr(params,attrgroupId);
        return R.ok().put("page",page);
    }

    @ApiOperation(value = "商品系统--平台属性--属性分组--关联--批量删除、删除")
    @PostMapping("/attr/relation/delete")
    public R deleteRelation(@RequestBody  AttrGroupRelationVo[] vos){
        pmsAttrService.deleteRelation(vos);
        return R.ok();
    }

    @ApiOperation(value = "根据分组ID查找关联的所有属性")
    @GetMapping("/{attrgroupId}/attr/relation")
    public R attrRelation(@PathVariable("attrgroupId") Long attrgroupId){
        List<PmsAttrEntity> entities =  pmsAttrService.getRelationAttr(attrgroupId);
        return R.ok().put("data",entities);
    }

    @ApiOperation(value = "属性分组--列表--所属分类")
    @RequestMapping("/list/{catelogId}")
    public R list(@RequestParam Map<String, Object> params, @PathVariable("catelogId") Long catelogId){
        PageUtils page = pmsAttrGroupService.queryByPage(params,catelogId);
        return R.ok().put("page", page);
    }


    @ApiOperation(value = "商品系统--平台属性--属性分组--表格信息")
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
        //根据id查询属性信息
		PmsAttrGroupEntity pmsAttrGroup = pmsAttrGroupService.getById(attrGroupId);
        //获取所属分类ID
        Long catelogId = pmsAttrGroup.getCatelogId();
        //根据所属分类ID获取  当前分类分支所有ID数据
        Long[] path = pmsCategoryService.findCatelogPath(catelogId);
        pmsAttrGroup.setCatelogPath(path);
        return R.ok().put("attrGroup", pmsAttrGroup);
    }

    @ApiOperation(value = "保存(默认)")
    @RequestMapping("/save")
    public R save(@RequestBody PmsAttrGroupEntity pmsAttrGroup){
		pmsAttrGroupService.save(pmsAttrGroup);

        return R.ok();
    }

    @ApiOperation(value = "修改(默认)")
    @RequestMapping("/update")
    public R update(@RequestBody PmsAttrGroupEntity pmsAttrGroup){
		pmsAttrGroupService.updateById(pmsAttrGroup);
        return R.ok();
    }

    @ApiOperation(value = "删除(默认)")
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		pmsAttrGroupService.removeByIds(Arrays.asList(attrGroupIds));
        return R.ok();
    }

}
